﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PmiScale.CUiPMIScaleCharacterSizeCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.PMI.PmiScale.Resources;
using EngineeringInternalExtension;
using EngineeringInternalExtension.CommandParameters;
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.PmiScale;

public class CUiPMIScaleCharacterSizeCmd : TxSpinBoxButtonCommand
{
  private string m_charLabel;
  private string m_unitLabel;
  private float m_size;
  private TxPmi m_selectedPmi;
  private bool m_scaling;
  private CApPMIScaleEnabler m_pmiScaleEnabler;
  private CApPMIScaleApp m_pmiScale;

  public CUiPMIScaleCharacterSizeCmd()
  {
    this.m_charLabel = PMIScaleStringTable.CHAR_LABEL;
    this.m_unitLabel = TxApplication.Options.Units.LinearUnitName;
    this.m_pmiScale = new CApPMIScaleApp();
    this.m_pmiScaleEnabler = new CApPMIScaleEnabler((TxPmiTextDisplayMode) 0);
    this.ResetEditBoxText();
  }

  public virtual bool Connect()
  {
    this.RegisterSelectionEvents();
    this.RegisterUnitsEvents();
    return true;
  }

  public virtual void Disconnect()
  {
    this.UnRegisterSelectionEvents();
    this.UnRegisterUnitsEvents();
    this.UnregisterPmiScalingEvents();
  }

  public virtual void Execute(TxSpinBoxCommandParameters spinBoxParams)
  {
    bool flag1 = false;
    bool flag2 = true;
    float num1 = 0.0f;
    try
    {
      num1 = this.ParseToFloat(spinBoxParams.Text);
      if ((double) num1 > 0.0)
      {
        if ((double) num1 <= base.Maximum)
          flag1 = true;
      }
    }
    catch
    {
    }
    if (flag1)
    {
      float linearMultiplier = (float) TxApplication.Options.Units.LinearMultiplier;
      float characterSize = num1 * linearMultiplier;
      if (spinBoxParams.InputMethod == 1)
      {
        float num2 = num1 - this.m_size;
        flag2 = (double) num2 <= 0.0 ? this.m_pmiScale.GetMinCharacterSizeFromSelection(ref characterSize) : this.m_pmiScale.GetMaxCharacterSizeFromSelection(ref characterSize);
        if (flag2)
        {
          num1 = (float) (Math.Floor((double) (characterSize / linearMultiplier) / base.Increment) * base.Increment);
          if ((double) num2 > 0.0)
            num1 += (float) base.Increment;
          else if ((double) num2 < 0.0)
            num1 -= (float) base.Increment;
          characterSize = num1 * linearMultiplier;
        }
      }
      this.m_scaling = true;
      this.m_pmiScale.SetCharacterSizeForSelection((double) characterSize);
      this.m_scaling = false;
      if (flag2)
      {
        this._text = $"{this.m_charLabel} {Math.Round((double) num1, TxApplication.Options.Units.LinearDigitsAfterDecimalPoint).ToString()} {this.m_unitLabel}";
        this._value = (double) num1;
        this.m_size = num1;
        this.FireUpdateContentRequested();
      }
    }
    if (flag1 && flag2)
      return;
    this.UpdateEditBoxText();
  }

  public virtual string Category => PMIScaleStringTable.PMI_SCALE_CATEGORY;

  public virtual string Name => PMIScaleStringTable.PMI_CHARACTER_SIZE;

  public virtual string Description => PMIScaleStringTable.PMI_CHARACTER_SIZE_DESCRIPTION;

  public virtual string Tooltip => PMIScaleStringTable.PMI_CHARACTER_SIZE_TOOLTIP;

  public virtual bool Licensed => true;

  public virtual bool ShowMissingLicenseMessage => true;

  public virtual int Width
  {
    get
    {
      int width = base.Width;
      try
      {
        width = int.Parse(PMIScaleStringTable.PMI_CHARACTER_SIZE_WIDTH);
      }
      catch
      {
      }
      return width;
    }
  }

  public virtual double Increment => this.m_pmiScale.CharacterSizeIncrement;

  public virtual double Minimum => this.m_pmiScale.CharacterSizeMinVal;

  public virtual double Maximum
  {
    get => this.m_pmiScale.CharacterSizeMaxVal / TxApplication.Options.Units.LinearMultiplier;
  }

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_pmiScaleEnabler;

  private void RegisterSelectionEvents()
  {
    TxSelection activeSelection = TxApplication.ActiveSelection;
    activeSelection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.OnSelectionChanged);
    activeSelection.Cleared += new TxSelection_ClearedEventHandler(this.OnSelectionChanged);
  }

  private void UnRegisterSelectionEvents()
  {
    TxSelection activeSelection = TxApplication.ActiveSelection;
    activeSelection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.OnSelectionChanged);
    activeSelection.Cleared -= new TxSelection_ClearedEventHandler(this.OnSelectionChanged);
  }

  private void OnSelectionChanged(object sender, EventArgs args)
  {
    this.RegisterPmiScalingEvents();
    this.UpdateEditBoxText();
  }

  private void RegisterUnitsEvents()
  {
    TxApplication.Options.Units.LinearUnitsChanged += new TxUnitsOptions_LinearUnitsChangedEventHandler(this.OnUnitsChanged);
  }

  private void UnRegisterUnitsEvents()
  {
    TxApplication.Options.Units.LinearUnitsChanged -= new TxUnitsOptions_LinearUnitsChangedEventHandler(this.OnUnitsChanged);
  }

  private void OnUnitsChanged(object sender, EventArgs args)
  {
    this.m_unitLabel = TxApplication.Options.Units.LinearUnitName;
    this.UpdateEditBoxText();
  }

  private void RegisterPmiScalingEvents()
  {
    this.UnregisterPmiScalingEvents();
    if (!TxApplication.ActiveSelection.HasSingleSelectionInAllItems())
      return;
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    if (((Collection<ITxObject>) allItems).Count <= 0 || !(((Collection<ITxObject>) allItems)[0] is TxPmi txPmi) || txPmi.TextDisplayMode != null)
      return;
    this.m_selectedPmi = txPmi;
    this.m_selectedPmi.TextScaled += new TxPmi_TextScaledEventHandler(this.OnTextScaled);
  }

  private void UnregisterPmiScalingEvents()
  {
    if (this.m_selectedPmi == null)
      return;
    this.m_selectedPmi.TextScaled -= new TxPmi_TextScaledEventHandler(this.OnTextScaled);
  }

  private void OnTextScaled(object sender, EventArgs args)
  {
    if (this.m_scaling)
      return;
    this.UpdateEditBoxText();
  }

  private void UpdateEditBoxText()
  {
    this.ResetEditBoxText();
    float linearMultiplier = (float) TxApplication.Options.Units.LinearMultiplier;
    float num = this.m_pmiScale.GetCharacterSizeFromSelection() / linearMultiplier;
    if ((double) num >= 0.0)
    {
      this._text = $"{this._text} {Math.Round((double) num, TxApplication.Options.Units.LinearDigitsAfterDecimalPoint).ToString()} {this.m_unitLabel}";
      this._value = (double) num;
      this.m_size = num;
    }
    this.FireUpdateContentRequested();
  }

  private void ResetEditBoxText()
  {
    this._text = this.m_charLabel;
    this._value = 10.0;
    this.m_size = 10f;
    this.FireUpdateContentRequested();
  }

  private float ParseToFloat(string strValue)
  {
    string lower1 = strValue.ToLower();
    string lower2 = this.m_charLabel.ToLower();
    string lower3 = this.m_unitLabel.ToLower();
    return float.Parse(lower1.Replace(lower2, "").Replace(lower3, "").Replace(" ", ""), (IFormatProvider) CultureInfo.InvariantCulture);
  }
}
